package Thread.DateSafety;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SDFsafetyTest {


    final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public String format(Date d) {
        return sdf.format(d);
    }

    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 50; i++) {
            PrintTimeNew pt = new PrintTimeNew();
            pt.threadName = i;
            es.execute(pt);
        }
        es.shutdown();
    }

}

class PrintTimeNew extends Thread {//时间转字符线程
    public int threadName;
    SDFsafetyTest ts = new SDFsafetyTest();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void run() {
        super.run();
        Date d = null;
        String strD = null;
        try {
            if (threadName % 2 == 0) {
                strD = "2012-02-02 12:12:12";
            } else {
                strD = "2012-03-30 12:12:12";
            }
            d = sdf.parse(strD);
        } catch (Exception e) {
        }

        for (int i = 0; i < 100; i++) {
            String sd = ts.format(d);
            try {
                Thread.sleep(1);
            } catch (Exception e) {
            }
            if (!sd.equals("2012-02-02 12:12:12") && !sd.equals("2012-03-30 12:12:12")) {
                System.out.println(threadName + ":  原:" + strD + "  新:" + sd);
            }
        }
    }

}
